﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emProject : emMasterDetailEdit
    {
        private data.baseDS.emProjEmployeeDataTable myDataTbl = new data.baseDS.emProjEmployeeDataTable();
        private data.baseDS.emProjectDataTable myProjectTbl = new data.baseDS.emProjectDataTable();
        public emProject()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er); ;
            }            
        }

        public override event EventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                emProjectDetail.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
                emProjectDetail.myOnError -= value;
            }
        }
        public override void Init()
        {
            base.Init();
            emProjectDetail.Init();
            SetData(myDataTbl);
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                application.dataLibs.LoadData(myDataTbl, emCode);
            }
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                application.dataLibs.FindAndCache(myProjectTbl, myDataTbl[idx].projectCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = emCode;
            }
            application.dataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state); 
            emProjectDetail.LockEdit(state);
        }
        public override bool CheckData()
        {
            return emProjectDetail.CheckData();
        }
        public override void SetFocus()
        {
            emProjectDetail.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emProjEmployeeRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emProjEmployeeRow)((DataRowView)this.gridDataSource.Current).Row;
                application.dataLibs.InitData(row);
            }
            emProjectDetail.SetDataSource(gridDataSource);
        }

        private void SetData(data.baseDS.emProjEmployeeDataTable tbl)
        {
            projectDataSource.DataSource = myProjectTbl;
            gridDataSource.DataSource = tbl;
            emProjectDetail.SetDataSource(gridDataSource);

            DataGridViewTextBoxColumn projectCodeColumn = new DataGridViewTextBoxColumn();
            DataGridViewComboBoxColumn projectNameColumn = new DataGridViewComboBoxColumn();
            
            this.detailGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] 
                                        {
                                            projectCodeColumn, projectNameColumn
                                        }
                                         );

            // 
            // projectCodeColumn
            // 
            projectCodeColumn.DataPropertyName = tbl.projectCodeColumn.ColumnName;
            projectCodeColumn.HeaderText = "Mã số";
            projectCodeColumn.Name = tbl.projectCodeColumn.ColumnName;
            projectCodeColumn.ReadOnly = true;
            projectCodeColumn.Width = 120;
            // 
            // projectNameColumn
            // 
            projectNameColumn.DataPropertyName = tbl.projectCodeColumn.ColumnName;
            projectNameColumn.DataSource = projectDataSource;            
            projectNameColumn.DisplayMember = myProjectTbl.titleColumn.ColumnName;
            projectNameColumn.ValueMember = myProjectTbl.projectCodeColumn.ColumnName;
            projectNameColumn.HeaderText = "Tên";
            projectNameColumn.Name = "projectName";
            projectNameColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            projectNameColumn.ReadOnly = true;

            common.system.AutoFitGridColumn(detailGrid,projectNameColumn.Name);
        }
        private data.baseDS.emProjectRow emProjectDetail_myOnNeedProjectData(object sender, string projectCode)
        {
            return application.dataLibs.FindAndCache(myProjectTbl, projectCode); 
        }
    }
}
